package com.example.zbh.lessona.reference;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

import com.example.zbh.lessona.R;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zbh on 15/11/30.
 */
public class ReferenceActivity extends Activity {
    private Map<String, SoftReference<Bitmap>> map = new HashMap<>();
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reference);
        imageView = (ImageView) findViewById(R.id.imageView);
        addBitMapToMap(R.mipmap.ee_1);
        imageView.setImageBitmap(getBitMapFromMap(R.mipmap.ee_1) == null ?
                BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher) :
                getBitMapFromMap(R.mipmap.ee_1));
    }
    public void addBitMapToMap(int id ){
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), id);
        SoftReference<Bitmap> bitmapSoftReference = new SoftReference<Bitmap>(bitmap);
        map.put(id + "", bitmapSoftReference);

    }

    public Bitmap getBitMapFromMap(int id){
        SoftReference<Bitmap> bitmapSoftReference = map.get(id + "");
        if (bitmapSoftReference == null){
            return null;
        }
        Bitmap bitmap = bitmapSoftReference.get();
        return bitmap;
    }
}
